package cn.com.ghy.android.widget.report;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.ghy.android.bean.CustomerInfoBean;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.OrderOpeService;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:发送传真页面
 * @author wuz  @date 2011-6-15 09:15:10
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class OrderFaxSend extends Activity {

	private TextView labTxtOrderId = null;
	private TextView labTxtFaxstate = null;

	private EditText txtFax = null;

	private Button btnFax = null;
	private ImageButton btnBack = null;

	private LinearLayout linCustomer = null;
	private ProgressDialog progressDialog = null;

	private Intent intent = null;
	private Bundle bundle = null;
	private CustomerInfoBean customerInfo = null;
	private LoginInfoBean loginInfo = null;
	private String fax = "";
	private String faxstate = "";

	private HttpThread httpThread = null;

	private DisposeException disposeException = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.order_fax_send);
		getSession();
		setToolBar();
		findView();
		init();
		addListener();
	}

	private void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		loginInfo = ((ComStaticApp) this.getApplication()).getLoginInfo();
		bundle = this.getIntent().getExtras();
		customerInfo = (CustomerInfoBean) bundle.getSerializable("customerInfo");
	}

	private void setToolBar() {
		View appToolBar = findViewById(R.id.appToolBar);
		ImageButton btnList = (ImageButton) appToolBar.findViewById(R.id.btnList);
		btnList.setVisibility(View.GONE);
		btnBack = (ImageButton) appToolBar.findViewById(R.id.btnBack);
	}

	private void findView() {
		labTxtOrderId = (TextView) this.findViewById(R.id.labTxtOrderId);
		labTxtFaxstate = (TextView) this.findViewById(R.id.labTxtFaxstate);

		txtFax = (EditText) this.findViewById(R.id.txtFax);

		btnFax = (Button) this.findViewById(R.id.btnFax);

		linCustomer = (LinearLayout) this.findViewById(R.id.linCustomer);
	}

	private void init() {
		labTxtOrderId.setText(customerInfo.getOrderReportInfoBean().getOrderid());
		LinearLayout.LayoutParams layoutParamsLab = new LinearLayout.LayoutParams(0,
				LayoutParams.WRAP_CONTENT);
		layoutParamsLab.setMargins(0, 0, 3, 0);
		layoutParamsLab.weight = 1;
		TextView tvLab = new TextView(OrderFaxSend.this);
		tvLab.setGravity(Gravity.RIGHT);
		tvLab.setTextSize(15);

		LinearLayout.LayoutParams layoutParamsTxt = new LinearLayout.LayoutParams(0,
				LayoutParams.WRAP_CONTENT);
		layoutParamsTxt.setMargins(0, 0, 10, 0);
		layoutParamsTxt.weight = 3;
		TextView tvTxt = new TextView(OrderFaxSend.this);
		tvTxt.setGravity(Gravity.LEFT);
		tvTxt.setTextSize(15);

		if (null != customerInfo.getJxsid() && null != customerInfo.getJxsname()) {
			tvLab.setText("经销商:");
			tvTxt.setText(customerInfo.getJxsid() + " " + customerInfo.getJxsname());
		} else if (null != customerInfo.getFxsid() && null != customerInfo.getFxsname()) {
			tvLab.setText("分销商:");
			tvTxt.setText(customerInfo.getFxsid() + " " + customerInfo.getFxsname());
		}
		linCustomer.addView(tvLab, layoutParamsLab);
		linCustomer.addView(tvTxt, layoutParamsTxt);

		fax = (null == customerInfo.getFax() ? "" : customerInfo.getFax());
		txtFax.setText(fax);
		String faxstate = (null == customerInfo.getFaxstate() ? "" : customerInfo.getFaxstate());
		labTxtFaxstate.setText(faxstate);
	}

	private void addListener() {
		btnFax.setOnClickListener(btnListener);
		btnBack.setOnClickListener(btnListener);
	}

	private boolean getData() {
		fax = txtFax.getText().toString().trim();
		if ("".equals(txtFax.getText().toString().trim())) {
			Toast.makeText(OrderFaxSend.this, "请输入传真号码!", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	private View.OnClickListener btnListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.btnFax :
					if (!getData()) {
						return;
					}
					new AlertDialog.Builder(OrderFaxSend.this).setTitle("提示信息")
							.setMessage("确认发送传真?")
							.setPositiveButton("确定", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dlg, int sumthin) {
									httpThread = new HttpThread(handler);
									httpThread.doStart();
								}
							}).setNeutralButton("取消", null).show();
					break;
				case R.id.btnBack :
					OrderFaxSend.this.finish();
					break;
			}

		}
	};

	private class HttpThread extends Thread {
		private Handler handle = null;

		public HttpThread(Handler hander) {
			handle = hander;
		}

		public void doStart() {
			progressDialog = new ProgressDialog(OrderFaxSend.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在发送传真，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}

		public void run() {
			Message message = null;
			try {
				String result = null;
				OrderOpeService service = new OrderOpeService();
				result = service.OrderFaxSend(customerInfo.getOrderReportInfoBean().getOrderid(),
						fax, loginInfo);
				progressDialog.dismiss();
				message = handle.obtainMessage();
				bundle = new Bundle();
				message.what = CommonStatic.STATE_NORMAL;
				bundle.putString("result", result);
				message.setData(bundle);
				handle.sendMessage(message);
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message m) {
			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					final String result = m.getData().getString("result");
					if ("OK".equals(result)) {
						labTxtFaxstate.setText("Y");
						faxstate = "Y";
						new AlertDialog.Builder(OrderFaxSend.this).setTitle("提示信息")
								.setMessage("发送传真成功!")
								.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dlg, int sumthin) {
										intent = new Intent();
										bundle = new Bundle();
										bundle.putString("result", result);
										bundle.putString("faxstate", faxstate);
										bundle.putString("fax", fax);
										intent.putExtras(bundle);
										OrderFaxSend.this.setResult(RESULT_OK, intent);
										OrderFaxSend.this.finish();
									}
								}).show();
					} else {
						new AlertDialog.Builder(OrderFaxSend.this).setTitle("提示信息")
								.setMessage(result).setNeutralButton("关闭", null).show();
					}
					break;
			}
		}
	};
}
